<?php

namespace StudyBuddy\Controllers;

use StudyBuddy\WebPage;
use StudyBuddy\Paginator;
use StudyBuddy\Template\Urhere;
use StudyBuddy\Request;
use StudyBuddy\Responder;

/**
 * Controller for displaying page with
 * all available tags for all questions
 * List can be sorted by name, by count and by latest question with
 * this tag
 *
 * Results are paginated
 * A search form will also be on the page to allow to find specific tag
 * Later the search will be ajaxified like on Stackoverflow
 */
class Viewqtags extends Viewquestions
{


	/**
	 * Indicates the current tab
	 *
	 * @var string
	 */
	protected $qtab = 'tags';

	protected $pagerPath = '/tags/name';

	protected $PER_PAGE = 60;

	/**
	 * Pagination links on the page
	 * will not be handled by Ajax
	 *
	 * @var bool
	 */
	protected $notAjaxPaginatable = false;

	/**
	 * Select items according to conditions passed in GET
	 * Conditions can be == 'unanswered', 'hot', 'recent' (default)
	 */
	protected function getCursor(){

		$aFields = array('tag', 'i_count', 'hts');

		$cond = $this->oRequest->get('cond', 's', 'popular');
		d('cond: '.$cond);

		/**
		 * Default sort is by timestamp Descending
		 * meaning most recent should be on top
		 *
		 */
		$sort = array('tag' => 1);
		/**
		 * @todo translate this title later
		 *
		 */
		$this->title = 'Tags';

		switch($cond){
			/**
			 * Hot is strictly per views
			 */
			case 'popular':
				$sort = array('i_count' => -1);
				$this->title = 'Popular tags';
				$this->pagerPath = '/tags/popular';
				d('cp');
				break;


				/**
				 * Most answers/comments/views
				 * There is an activity counter
				 * 1 point per view, 10 point per comment,
				 * 50 points per answer
				 * but still limit to 30 days
				 * Cache Tags for 1 day only
				 * uncache onQuestionVote, onQuestionComment
				 */
			case 'recent':
				$this->title = 'Recent tags';
				$sort = array('i_ts' => -1);
				$this->pagerPath = '/tags/recent';
				d('cp');
				break;


				/**
				 * Default is all questions
				 * Tags are qrecent
				 * uncache qrecent onNewQuestion only!
				 */
			default:
				$sort = array('tag' => 1);
		}

		$this->typeDiv = Urhere::factory($this->oRegistry)->get('tplTagsort', $cond);

		$this->oCursor = $this->oRegistry->Mongo->QUESTION_TAGS->find(array('i_count' => array('$gt' => 0)), $aFields);
		$this->count = $this->oCursor->count(true);
		d('$this->oCursor: '.gettype($this->oCursor).' $this->count: '.$this->count);
		$this->oCursor->sort($sort);

		return $this;
	}



	/**
	 * No cache headers for this page but
	 * if this is an ajax request then return
	 * the html we have so far as it does not make
	 * sense to process any further methods
	 *
	 * @see wwwViewquestions::sendCacheHeaders()
	 */
	protected function sendCacheHeaders(){

		if(Request::isAjax()){
			$sQdivs = \tplTagslist::loop($this->oCursor);
			Responder::sendJSON(array('paginated' => '<div class="tags_wrap">'.$sQdivs.$this->pagerLinks.'</div>'));
		}

		return $this;
	}


	/**
	 * Make html block for
	 * the top-right position
	 * For this type of page it
	 * does not actually contain any count, just
	 * an informative text message
	 *
	 * (non-PHPdoc)
	 * @see StudyBuddy\Controllers.Viewquestions::makeCounterBlock()
	 */
	protected function makeCounterBlock(){
		/**
		 * @todo
		 * Translate string
		 */
		$text = 'Unique Tags';

		$description = 'A tag is a keyword or label that categorizes your question with other, similar questions. Using the right tags makes it easier for others to find and answer your question.';

		$this->aPageVars['topRight'] = \tplCounterblock::parse(array($this->count, $text, $description), false);

		return $this;
	}


	/**
	 * (non-PHPdoc)
	 * @see StudyBuddy\Controllers.Viewquestions::makeQlistBody()
	 */
	protected function makeQlistBody(){

		/**
		 * @todo pass false to loop
		 * but we MUST be sure that we have consistent
		 * field names in QUESTION_TAGS collection and that
		 * they are always in the same order, which would be
		 * the case normally as long as they are inserted using
		 * the same class
		 *
		 */
		$sQdivs = \tplTagslist::loop($this->oCursor);
		$sQlist = \tplQlist::parse(array($this->typeDiv, $sQdivs.$this->pagerLinks, '', $this->notAjaxPaginatable), false);
		$this->aPageVars['body'] = $sQlist;
		/**
		 * In case of Ajax can just send out sQlist as result
		 */
		return $this;
	}
}

